'use strict';
let url = '';
let name = '';
let url1 = '';
let name1 = '';
let url2 = '';
let name2 = '';
const Controller = require('egg').Controller;
const JWT = require('jsonwebtoken')
const OSS = require('ali-oss')


const OSSPage = new OSS({
  region: 'oss-cn-hangzhou',
  accessKeyId: 'LTAI5tBot9RPPVJwBdqZn9XF',
  accessKeySecret: 'bBEiHrbajyul6TIj8Yg7H67VPUPCRR',
  bucket: 'haibaomxk'
})
class HomeController extends Controller {
  // 登录
  async login() {
    const { ctx } = this;
    let {
      username, password
    } = ctx.request.body;
    const post = await this.app.mysql.get('list', {
      username: username
    });
    if (post) {
      const passif = await this.app.mysql.get('list', {
        username: username,
        password: password
      });
      if (passif) {
        ctx.body = {
          code: 200,
          mes: '登录成功',
          token: JWT.sign(passif.id, 'mxk'),
          token1: passif.type
        }
      } else {
        ctx.body = {
          code: 401,
          mes: '密码错误'
        }
      }
    } else {
      ctx.body = {
        code: 404,
        mes: '用户名不存在'
      }
    }
  }
  // 注册
  async zhu() {
    const {
      ctx
    } = this;
    let {
      ruleForm, data1
    } = ctx.request.body;
    let { username, password } = ruleForm


    const post = await this.app.mysql.get('list', { username: username });
    if (post) {
      ctx.body = {
        code: 502,
        mes: '用户名已存在'
      }
    } else {
      const result = await this.app.mysql.insert('list', { username: username, type: '访客', pan: '可用', jin: '禁用', sou: '授权', password: password, data: data1._value });
      if (result) {
        ctx.body = {
          code: 200,
          mes: '注册成功!!!'
        }
      }
    }
  }
  // 获取用户信息
  async user() {
    const {
      ctx
    } = this;
    let {
      token
    } = ctx.request.body;
    const id = JWT.verify(token, 'mxk')
    const data = await this.app.mysql.get('list', { id: id });
    if (data) {
      ctx.body = {
        code: 200,
        data: data
      }
    }
  }

  async zui() {
    const { ctx, app } = this

    const data = await app.mysql.select('zuixin')
    ctx.body = {
      code: 200,
      data: data,
      total: data.length
    }
  }

  async lis() {
    const { ctx, app } = this
    const { token } = ctx.request.body
    const data = await app.mysql.select('list')

    const id = JWT.verify(token, 'mxk')
    const list = await app.mysql.get('list', { id: id })

    if (list.type === '管理员') {
      ctx.body = {
        code: 200,
        data: data,
        total: data.length
      }
    } else {
      console.log('不是管理员');
      ctx.body = {
        code: 404
      }
    }
  }

  async jin() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    console.log(id);
    const list = await app.mysql.get('list', { id: id })
    console.log(list.jin);
    if (list.jin === '禁用') {
      await app.mysql.update('list', { id: id, pan: '已锁定', jin: '启用' })
      ctx.body = {
        code: 200

      }
    } else if (list.jin === '启用') {
      await app.mysql.update('list', { id: id, pan: '可用', jin: '禁用' })
      ctx.body = {
        code: 200

      }
    }
  }
  async shou() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    console.log(id);
    const list = await app.mysql.get('list', { id: id })
    console.log(list.jin);
    if (list.sou === '授权') {
      await app.mysql.update('list', { id: id, type: '管理员', sou: '解除授权' })
      ctx.body = {
        code: 200
      }
    } else if (list.sou === '解除授权') {
      await app.mysql.update('list', { id: id, type: '访客', sou: '授权' })
      ctx.body = {
        code: 200

      }
    }
  }


  async Sou() {
    const { ctx, app } = this
    const { token } = ctx.request.body
    // console.log(token);
    const id = JWT.verify(token, 'mxk')
    const list = await app.mysql.get('list', { id: id })
    if (list.pan === '可用') {
      ctx.body = {
        code: 200
      }
    } else {
      ctx.body = {
        code: 404
      }
    }

  }

  async ls() {
    const { ctx, app } = this
    const { input, input1, value, value1 } = ctx.request.body
    console.log(input._value, input1, value, value1)

    if (input._value !== '' || input1._value !== '' || value._value !== '' || value1._value !== '') {
      const data = await app.mysql.query(`select * from list where username like '%${input._value}%' and you like '%${input1._value}%' and type like '%${value._value}%' and pan like '%${value1._value}%'  `)

      ctx.body = {
        code: 1,
        data: data,
        total: data.length
      }
    } else {
      const data = await app.mysql.select('list')
      ctx.body = {
        code: 1,
        data: data
      }
    }
  }

  async pin() {
    const { ctx, app } = this
    const data = await app.mysql.select('ping')
    ctx.body = {
      code: 1,
      data: data,
      total: data.length

    }
  }

  async tong() {
    const { ctx, app } = this
    const { id, token } = ctx.request.body
    const data = await app.mysql.get('ping', { id: id })
    console.log(token);
    const id1 = JWT.verify(token, 'mxk')
    const data1 = await app.mysql.get('list', { id: id1 })
    console.log(data1);
    if (data1.type === '管理员') {
      if (data.tyoe === '通过') {
        ctx.body = {
          code: 404
        }
      } else {
        await app.mysql.update('ping', { id: id, tyoe: '通过' })
        ctx.body = {
          code: 200,
        }
      }
    } else {
      ctx.body = {
        code: 503
      }
    }
  }

  async tong1() {
    const { ctx, app } = this
    const { id, token } = ctx.request.body
    const data = await app.mysql.get('ping', { id: id })
    console.log(token);
    const id1 = JWT.verify(token, 'mxk')
    const data1 = await app.mysql.get('list', { id: id1 })
    console.log(data1);
    if (data1.type === '管理员') {
      if (data.tyoe === '未通过') {
        ctx.body = {
          code: 404
        }
      } else {
        await app.mysql.update('ping', { id: id, tyoe: '未通过' })
        ctx.body = {
          code: 200,
        }
      }
    } else {
      ctx.body = {
        code: 503
      }
    }
  }

  async lis1() {
    const { ctx, app } = this
    const { token, textarea } = ctx.request.body
    const id = JWT.verify(token, 'mxk')
    const data = await app.mysql.get('list', { id: id })
    console.log(data);
    if (data.type === '管理员') {
      await app.mysql.insert('ping', { username: data.username,lian:'123',data:'2022',fu:'无',ting:'通过',ju:'拒绝', title: textarea._value, type: '已通过' })
      ctx.body = {
        code: 200
      }
    } else {
      ctx.body = {
        code: 404
      }
    }

  }

  async pan() {
    const { ctx, app } = this
    const { token } = ctx.request.body
    const id = JWT.verify(token, 'mxk')
    const data = await app.mysql.get('list', { id: id })
    console.log(data);
    if (data.type === '管理员') {

      ctx.body = {
        code: 200
      }
    } else {
      ctx.body = {
        code: 404
      }
    }
  }

  async dele() {
    const { ctx, app } = this
    const { id, token } = ctx.request.body
    const id1 = JWT.verify(token, 'mxk')

    const data = await app.mysql.get('list', { id: id1 })
    console.log(data);
    if (data.type === '管理员') {
      await app.mysql.delete('pinlun', { id: id })
      ctx.body = {
        code: 200
      }
    } else {
      ctx.body = {
        code: 404
      }
    }
  }

  async tiao() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('zuixin', { id: id })
    if (data) {
      ctx.body = {
        code: 200,
        data: data
      }
    } else {
      ctx.body = {
        code: 1
      }
    }
  }

  async souzui() {
    const { ctx, app } = this
    const { input, value, value1 } = ctx.request.body
    console.log(input, value, value1);
    if (input._value !== '' || value._value !== '' || value1._value !== '') {
      const data = await app.mysql.query(`select * from zuixin where username like '%${input._value}%' and type like '%${value._value}%' and fity like '%${value1._value}%'  `)

      ctx.body = {
        code: 1,
        data: data,
        total: data.length
      }
    } else {
      const data = await app.mysql.select('zuixin')
      ctx.body = {
        code: 1,
        data: data
      }
    }

  }

  async tuijian() {
    const { ctx, app } = this
    const { id2 } = ctx.request.body
    const data1 = await app.mysql.get('zuixin', { id: id2 })
    console.log(data1.tui);
    if (data1.tui === '首焦推荐') {
      await app.mysql.update('zuixin', { id: data1.id, tui: '撤销首焦' })
      ctx.body = {
        code: 1
      }
    } else if (data1.tui === '撤销首焦') {
      await app.mysql.update('zuixin', { id: id2, tui: '首焦推荐' })

    }
  }

  async delet() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    console.log(id);

    await app.mysql.delete('zuixin', { id: id })
    ctx.body = {
      code: 200
    }
  }
  async hli() {
    const { ctx, app } = this
    const data = await app.mysql.select('guanli')
    ctx.body = {
      code: 1,
      data: data,
      total: data.length
    }

  }

  async gai() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    console.log(id);
    const data1 = await app.mysql.get('guanli', { id: id })
    console.log(data1);
    if (data1.caozuo === '发布') {
      await app.mysql.update('guanli', { id: data1.id, type: '已发布', caozuo: '下线' })
      ctx.body = {
        code: 1
      }
    } else if (data1.caozuo === '下线') {
      await app.mysql.update('guanli', { id: data1.id, type: '草稿', caozuo: '发布' })
      ctx.body = {
        code: 1
      }
    }

  }

  async ti() {
    const { ctx, app } = this
    const { input, id } = ctx.request.body
    console.log(input, id);
    const data = await app.mysql.get('zuixin', { id, id })
    if (data) {
      await app.mysql.update('zuixin', { id: id, username: input._value })
      ctx.body = {
        code: 200
      }
    } else {
      ctx.body = {
        code: 1
      }
    }

  }

  // 文章管理添加操作
  async tianjia() {
    const { ctx, app } = this
    const { input, data } = ctx.request.body
    console.log(input, data);
    await app.mysql.insert('zuixin', { username: input._value, title: '', img: '', data: data._value, type: '已发布', fity: '前端', biao: '', num: '0', love: '0', tui: '首焦推荐' })
    ctx.body = {
      code: 1
    }
  }

  // 文章管理删除操作
  async shanchu() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    console.log(id);
    await app.mysql.delete('guanli', { id: id })
    ctx.body = {
      code: 1
    }
  }

  // 页面管理添加操作
  async jianq() {
    const { ctx, app } = this
    const { data, input, input1, input2, input3 } = ctx.request.body
    console.log(data, input, input1, input2, input3);
    await app.mysql.insert('guanli', { username: input._value, fengmian: input1._value, path: input2._value, shunxu: input3._value, data: data._value, type: '已发布', num: '1', caozuo: '下线' })
    ctx.body = {
      code: 1
    }
  }

  // 页面管理搜索操作
  async yesou() {
    const { ctx, app } = this
    const { input, input1, value,data1 } = ctx.request.body
    console.log(input, input1, value,data1);

    if (input._value !== '' || value._value !== '' || input1._value !== '') {
      const data = await app.mysql.query(`select * from guanli where username like '%${input._value}%' and path like '%${input1._value}%' and type like '%${value._value}%'  `)
       await app.mysql.insert('sou',{ci:input._value,num:'1',data:data1._value})
      ctx.body = {
        code: 1,
        data: data,
        total: data.length
      }
    } else {
      const data = await app.mysql.select('zuixin')
      ctx.body = {
        code: 1,
        data: data,
        total: data.length
      }
    }

  }

  async yon() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('guanli', { id: id })
    ctx.body = {
      code: 1,
      data: data
    }
  }
  // 页面管理修改操作
  async xiugai() {
    const { ctx, app } = this
    const { input, id } = ctx.request.body
    await app.mysql.update('guanli', { id: id, username: input._value })
    console.log(input, id);
    ctx.body = {
      code: 1
    }
  }

  async zhilie() {
    const { ctx, app } = this
    const data = await app.mysql.select('zhishi')

    ctx.body = {
      code: 1,
      data: data
    }
  }

  async zhishang() {
    const { ctx, app } = this
    const { id } = ctx.request.body

    const data = await app.mysql.get('zhishi', { id: id })
    console.log(data);
    if (data.type === '发布上线') {
      await app.mysql.update('zhishi', { id: id, type: '设为草稿' })
      ctx.body = {
        code: 1,
      }
    } else if (data.type === '设为草稿') {
      await app.mysql.update('zhishi', { id: id, type: '发布上线' })
      ctx.body = {
        code: 1,
      }
    }

  }


  async zhixin() {
    const { ctx, app } = this
    const { id } = ctx.request.body

    const data = await app.mysql.get('zhishi', { id: id })
    // console.log(data);

    ctx.body = {
      code: 1,
      data: data
    }
  }
  // 知识小册修改
  async zhigeng() {
    const { ctx, app } = this
    const { name, textarea, tupian, id1 } = ctx.request.body
    // console.log(name, textarea, tupian, id1);
    await app.mysql.update('zhishi', { id: id1._value, username: name._value, title: textarea._value, img: tupian._value })
    // console.log(data);
    ctx.body = {
      code: 1,
      // data:data
    }
  }
  // 知识小册删除
  async zhishan() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    console.log(id);
    await app.mysql.delete('zhishi', { id: id })

    ctx.body = {
      code: 1,

    }
  }
  //  图片上传
  async upload() {
    const { ctx, app } = this
    const file = ctx.request.files[0]
    const data = await OSSPage.put(file.filename, file.filepath)
    console.log(data);
    url1 = data.url
    name1 = file.filename
    console.log(url1);
    ctx.body = {
      code: 1,
      data: data
    }
  }
  // 知识小册创建
  async zhichuang() {
    const { ctx, app } = this
    const { name, tupian, textarea } = ctx.request.body
    // console.log(name, tupian, textarea);
    await app.mysql.insert('zhishi', { username: name._value, title: textarea._value, img: url1, type: '发布上线' })
    ctx.body = {
      code: 1,

    }
  }


  async zhisou() {
    const { ctx, app } = this
    const { value, input } = ctx.request.body
    console.log(value, input);
    if (input._value !== '' || value._value !== '') {
      const data = await app.mysql.query(`select * from zhishi where username like '%${input._value}%' and type like '%${value._value}%' `)
      ctx.body = {
        code: 1,
        data: data,

      }
    } else {
      const data = await app.mysql.select('zuixin')
      ctx.body = {
        code: 1,
        data: data,

      }
    }

  }




  //  海报管理图片上传
  async upload1() {
    const { ctx, app } = this
    const file = ctx.request.files[0]
    const data = await OSSPage.put(file.filename, file.filepath)
    console.log(data);
    url = data.url
    name = file.filename
    console.log(url);
    await app.mysql.insert('tu', { img: url, username: name })

    ctx.body = {
      code: 1,
      data: data
    }
  }
  async upload2() {
    const { ctx, app } = this
    const file = ctx.request.files[0]
    const data = await OSSPage.put(file.filename, file.filepath)
    console.log(data);
    url = data.url
    name = file.filename
    console.log(url);
    await app.mysql.insert('wen', { img: url, username: name,data:'09' })

    ctx.body = {
      code: 1,
      data: data
    }
  }
  async bao() {
    const { ctx, app } = this
 
    // await app.mysql.insert('tu', { img: url, username: name })

    ctx.body = {
      code: 1,

    }
  }

  // 获取海报列表
  async haiList() {
    const { ctx, app } = this
    const data = await app.mysql.select('tu')
    ctx.body = {
      code: 1,
      data: data
    }
  }
  // 获取海报搜索列表
  async haisou() {
    const { ctx, app } = this
    const { input } = ctx.request.body
    console.log(input);
    const data = await app.mysql.query(`select * from tu where username like '%${input._value}%'  `)
    console.log(data);
    ctx.body = {
      code: 1,
      data: data
    }
  }

  // 评论管理列表
  async listping() {
    const { ctx, app } = this

    const data = await app.mysql.select('ping')
    ctx.body = {
      code: 1,
      data,
      total:data.length
    }
  }

  // 评论管理通过
  async pingtong() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('ping', { id: id })
    console.log(data);
    if (data.ting === '通过') {
      await app.mysql.update('ping', { id: id, type: '已通过' })
      ctx.body = {
        code: 1,
      }
    } else {

      ctx.body = {
        code: 403,
      }
    }

  }
  // 评论管理拒绝
  async pingju() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    const data = await app.mysql.get('ping', { id: id })
    console.log(data);
    if (data.ju === '拒绝') {
      await app.mysql.update('ping', { id: id, type: '未通过' })
      ctx.body = {
        code: 1,
      }
    } else {
      ctx.body = {
        code: 403,
      }
    }
  }

  // 评论管理添加
  async pingtian() {
    const { ctx, app } = this
    const { textarea, data1, token } = ctx.request.body
    const id = JWT.verify(token, 'mxk')
    const list = await app.mysql.get('list', { id: id })
    await app.mysql.insert('ping', { type: '已通过', username: list.username, lian: '123', data: data1._value, title: textarea._value, fu: '无', ting: '通过', ju: '拒绝' })
    ctx.body = {
      code: 1
    }
  }


  // 评论管理删除
  async pingdel() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    console.log(id);
    await app.mysql.delete('ping', { id: id })
    ctx.body = {
      code: 1
    }
  }

  // 评论管理搜索
  async pingsou() {
    const { ctx, app } = this
    const { value, input, input1,data1 } = ctx.request.body
    console.log(value, input, input1);
    await app.mysql.insert('sou',{ci:input._value,num:'1',data:data1._value})
    const data = await app.mysql.query(`select * from ping where username like '%${input._value}%' and lian like '%${input1._value}%' and type like '%${value._value}%' `)
    ctx.body = {
      code: 1,
      data: data
    }
  }
  // 邮件管理列表
  async youlist() {
    const { ctx, app } = this
    const data = await app.mysql.select('you')
    ctx.body = {
      code: 1,
      data: data
    }
  }


  // 邮件管理删除
  async youshan() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    await app.mysql.delete('you', { id: id })
    ctx.body = {
      code: 1,

    }
  }

  // 邮件管理搜索
  async yousou() {
    const { ctx, app } = this
    const { input, input1, input2,data } = ctx.request.body
    const data1 = await app.mysql.query(`select * from you where fa like '%${input._value}%' and shou like '%${input1._value}%' and zhu like '%${input2._value}%' `)
    await app.mysql.insert('sou', { ci: input._value, num: 1, data: data._value })
    ctx.body = {
      code: 1,
      data: data1
    }
  }

  async wenlist() {
    const { ctx, app } = this
    const data = await app.mysql.select('wen')
    ctx.body = {
      code: 1,
      data: data
    }
  }

  // 搜索记录列表
  async soulist() {
    const { ctx, app } = this
    const data = await app.mysql.select('sou')
    ctx.body = {
      code: 1,
      data: data,
      total:data.length
    }
  }




  // 搜索记录搜索
  async sousuo() {
    const { ctx, app } = this
    const { title, word, data } = ctx.request.body
    const data1 = await app.mysql.query(`select * from sou where ci like '%${title._value}%' and num like '%${word._value}%'  `)
    await app.mysql.insert('sou', { ci: word._value, num: 1, data: data._value })
    ctx.body = {
      code: 1,
      data: data1
    }
  }

  // 搜索记录删除
  async soudel() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    await app.mysql.delete('sou', { id: id })
    ctx.body = {
      code: 1,

    }
  }

  // 访问统计列表
  async fanglist() {
    const { ctx, app } = this
    const data = await app.mysql.select('fang')
    ctx.body = {
      code: 1,
      data: data
    }
  }
  // 访问统计删除
  async fangdel() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    console.log(id);
    await app.mysql.delete('fang', { id: id })
    ctx.body = {
      code: 1
    }
  }

  // 访问统计搜索
  async fangsou() {
    const { ctx, app } = this
    const { title, name, dz, sl, neihe, word, data } = ctx.request.body
    const data1 = await app.mysql.query(`select * from fang where ip like '%${title._value}%' and url like '%${name._value}%' and di like '%${dz._value}%' and num like '%${sl._value}%' and nei like '%${neihe._value}%' and liu like '%${word._value}%' `)
    await app.mysql.insert('sou', { ci: dz._value, num: 1, data: data._value })
    console.log(data);
    ctx.body = {
      code: 1,
      data: data1
    }
  }


  // 文章管理分类
  async getfen() {
    const { ctx, app } = this
    const data = await app.mysql.select('fen')
    ctx.body = {
      code: 1,
      data: data
    }
  }
  // 文章管理分类添加
  async fenjia() {
    const { ctx, app } = this
    const { input1, input2 } = ctx.request.body
    // console.log(input1,input2);
    await app.mysql.insert('fen', { username: input1._value, path: input2._value })
    ctx.body = {
      code: 1,
    }
  }
  // 文章管理分类删除
  async fendel() {
    const { ctx, app } = this
    const { id } = ctx.request.body

    await app.mysql.delete('fen', { id: id._value })
    ctx.body = {
      code: 1,
    }
  }

  // 文章管理分类更新
  async fengeng() {
    const { ctx, app } = this
    const { id, input1, input2 } = ctx.request.body

    console.log(id, input1, input2);
    await app.mysql.update('fen', { id: id._value, username: input1._value, path: input2._value })
    ctx.body = {
      code: 1,
    }
  }

  // 启用
  async qilist() {
    const { ctx, app } = this
    await app.mysql.update("list",{pan:"可用"})
    ctx.body = {
      code: 1,
    }
  }

  async tiaolist(){
    const { ctx, app } = this
    const { id} = ctx.request.body
   const data=await app.mysql.get('zhishi',{id:id})
    ctx.body = {
      code: 1,
      data
    }
  }
  async tiaogeng(){
    const { ctx, app } = this
    const { name,title,id} = ctx.request.body
   const data=await app.mysql.update('zhishi',{id:id,username:name._value,title:title._value})
    ctx.body = {
      code: 1,
      data
    }
  }
  
}

module.exports = HomeController;
